1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.SelectionModelT;
26 
27 public  import gobject.ObjectG;
28 public  import gobject.Signals;
29 public  import gtk.Bitset;
30 public  import gtk.c.functions;
31 public  import gtk.c.types;
32 public  import std.algorithm;
33 
34 
35 /**
36  * `GtkSelectionModel` is an interface that add support for selection to list models.
37  * 
38  * This support is then used by widgets using list models to add the ability
39  * to select and unselect various items.
40  * 
41  * GTK provides default implementations of the most common selection modes such
42  * as [class@Gtk.SingleSelection], so you will only need to implement this
43  * interface if you want detailed control about how selections should be handled.
44  * 
45  * A `GtkSelectionModel` supports a single boolean per item indicating if an item is
46  * selected or not. This can be queried via [method@Gtk.SelectionModel.is_selected].
47  * When the selected state of one or more items changes, the model will emit the
48  * [signal@Gtk.SelectionModel::selection-changed] signal by calling the
49  * [method@Gtk.SelectionModel.selection_changed] function. The positions given
50  * in that signal may have their selection state changed, though that is not a
51  * requirement. If new items added to the model via the
52  * [signal@Gio.ListModel::items-changed] signal are selected or not is up to the
53  * implementation.
54  * 
55  * Note that items added via [signal@Gio.ListModel::items-changed] may already
56  * be selected and no [signal@Gtk.SelectionModel::selection-changed] will be
57  * emitted for them. So to track which items are selected, it is necessary to
58  * listen to both signals.
59  * 
60  * Additionally, the interface can expose functionality to select and unselect
61  * items. If these functions are implemented, GTK's list widgets will allow users
62  * to select and unselect items. However, `GtkSelectionModel`s are free to only
63  * implement them partially or not at all. In that case the widgets will not
64  * support the unimplemented operations.
65  * 
66  * When selecting or unselecting is supported by a model, the return values of
67  * the selection functions do *not* indicate if selection or unselection happened.
68  * They are only meant to indicate complete failure, like when this mode of
69  * selecting is not supported by the model.
70  * 
71  * Selections may happen asynchronously, so the only reliable way to find out
72  * when an item was selected is to listen to the signals that indicate selection.
73  */
74 public template SelectionModelT(TStruct)
75 {
76 	/** Get the main Gtk struct */
77 	public GtkSelectionModel* getSelectionModelStruct(bool transferOwnership = false)
78 	{
79 		if (transferOwnership)
80 			ownedRef = false;
81 		return cast(GtkSelectionModel*)getStruct();
82 	}
83 
84 
85 	/**
86 	 * Gets the set containing all currently selected items in the model.
87 	 *
88 	 * This function may be slow, so if you are only interested in single item,
89 	 * consider using [method@Gtk.SelectionModel.is_selected] or if you are only
90 	 * interested in a few, consider [method@Gtk.SelectionModel.get_selection_in_range].
91 	 *
92 	 * Returns: a `GtkBitset` containing all the values currently
93 	 *     selected in @model. If no items are selected, the bitset is empty.
94 	 *     The bitset must not be modified.
95 	 */
96 	public Bitset getSelection()
97 	{
98 		auto __p = gtk_selection_model_get_selection(getSelectionModelStruct());
99 
100 		if(__p is null)
101 		{
102 			return null;
103 		}
104 
105 		return ObjectG.getDObject!(Bitset)(cast(GtkBitset*) __p, true);
106 	}
107 
108 	/**
109 	 * Gets the set of selected items in a range.
110 	 *
111 	 * This function is an optimization for
112 	 * [method@Gtk.SelectionModel.get_selection] when you are only
113 	 * interested in part of the model's selected state. A common use
114 	 * case is in response to the [signal@Gtk.SelectionModel::selection-changed]
115 	 * signal.
116 	 *
117 	 * Params:
118 	 *     position = start of the queired range
119 	 *     nItems = number of items in the queried range
120 	 *
121 	 * Returns: A `GtkBitset` that matches the selection state
122 	 *     for the given range with all other values being undefined.
123 	 *     The bitset must not be modified.
124 	 */
125 	public Bitset getSelectionInRange(uint position, uint nItems)
126 	{
127 		auto __p = gtk_selection_model_get_selection_in_range(getSelectionModelStruct(), position, nItems);
128 
129 		if(__p is null)
130 		{
131 			return null;
132 		}
133 
134 		return ObjectG.getDObject!(Bitset)(cast(GtkBitset*) __p, true);
135 	}
136 
137 	/**
138 	 * Checks if the given item is selected.
139 	 *
140 	 * Params:
141 	 *     position = the position of the item to query
142 	 *
143 	 * Returns: %TRUE if the item is selected
144 	 */
145 	public bool isSelected(uint position)
146 	{
147 		return gtk_selection_model_is_selected(getSelectionModelStruct(), position) != 0;
148 	}
149 
150 	/**
151 	 * Requests to select all items in the model.
152 	 *
153 	 * Returns: %TRUE if this action was supported and no fallback should be
154 	 *     tried. This does not mean that all items are now selected.
155 	 */
156 	public bool selectAll()
157 	{
158 		return gtk_selection_model_select_all(getSelectionModelStruct()) != 0;
159 	}
160 
161 	/**
162 	 * Requests to select an item in the model.
163 	 *
164 	 * Params:
165 	 *     position = the position of the item to select
166 	 *     unselectRest = whether previously selected items should be unselected
167 	 *
168 	 * Returns: %TRUE if this action was supported and no fallback should be
169 	 *     tried. This does not mean the item was selected.
170 	 */
171 	public bool selectItem(uint position, bool unselectRest)
172 	{
173 		return gtk_selection_model_select_item(getSelectionModelStruct(), position, unselectRest) != 0;
174 	}
175 
176 	/**
177 	 * Requests to select a range of items in the model.
178 	 *
179 	 * Params:
180 	 *     position = the first item to select
181 	 *     nItems = the number of items to select
182 	 *     unselectRest = whether previously selected items should be unselected
183 	 *
184 	 * Returns: %TRUE if this action was supported and no fallback should be
185 	 *     tried. This does not mean the range was selected.
186 	 */
187 	public bool selectRange(uint position, uint nItems, bool unselectRest)
188 	{
189 		return gtk_selection_model_select_range(getSelectionModelStruct(), position, nItems, unselectRest) != 0;
190 	}
191 
192 	/**
193 	 * Helper function for implementations of `GtkSelectionModel`.
194 	 *
195 	 * Call this when a the selection changes to emit the
196 	 * [signal@Gtk.SelectionModel::selection-changed] signal.
197 	 *
198 	 * Params:
199 	 *     position = the first changed item
200 	 *     nItems = the number of changed items
201 	 */
202 	public void selectionChanged(uint position, uint nItems)
203 	{
204 		gtk_selection_model_selection_changed(getSelectionModelStruct(), position, nItems);
205 	}
206 
207 	/**
208 	 * Make selection changes.
209 	 *
210 	 * This is the most advanced selection updating method that allows
211 	 * the most fine-grained control over selection changes. If you can,
212 	 * you should try the simpler versions, as implementations are more
213 	 * likely to implement support for those.
214 	 *
215 	 * Requests that the selection state of all positions set in @mask
216 	 * be updated to the respective value in the @selected bitmask.
217 	 *
218 	 * In pseudocode, it would look something like this:
219 	 *
220 	 * ```c
221 	 * for (i = 0; i < n_items; i++)
222 	 * {
223 	 * // don't change values not in the mask
224 	 * if (!gtk_bitset_contains (mask, i))
225 	 * continue;
226 	 *
227 	 * if (gtk_bitset_contains (selected, i))
228 	 * select_item (i);
229 	 * else
230 	 * unselect_item (i);
231 	 * }
232 	 *
233 	 * gtk_selection_model_selection_changed (model,
234 	 * first_changed_item,
235 	 * n_changed_items);
236 	 * ```
237 	 *
238 	 * @mask and @selected must not be modified. They may refer to the
239 	 * same bitset, which would mean that every item in the set should
240 	 * be selected.
241 	 *
242 	 * Params:
243 	 *     selected = bitmask specifying if items should be selected or unselected
244 	 *     mask = bitmask specifying which items should be updated
245 	 *
246 	 * Returns: %TRUE if this action was supported and no fallback should be
247 	 *     tried. This does not mean that all items were updated according
248 	 *     to the inputs.
249 	 */
250 	public bool setSelection(Bitset selected, Bitset mask)
251 	{
252 		return gtk_selection_model_set_selection(getSelectionModelStruct(), (selected is null) ? null : selected.getBitsetStruct(), (mask is null) ? null : mask.getBitsetStruct()) != 0;
253 	}
254 
255 	/**
256 	 * Requests to unselect all items in the model.
257 	 *
258 	 * Returns: %TRUE if this action was supported and no fallback should be
259 	 *     tried. This does not mean that all items are now unselected.
260 	 */
261 	public bool unselectAll()
262 	{
263 		return gtk_selection_model_unselect_all(getSelectionModelStruct()) != 0;
264 	}
265 
266 	/**
267 	 * Requests to unselect an item in the model.
268 	 *
269 	 * Params:
270 	 *     position = the position of the item to unselect
271 	 *
272 	 * Returns: %TRUE if this action was supported and no fallback should be
273 	 *     tried. This does not mean the item was unselected.
274 	 */
275 	public bool unselectItem(uint position)
276 	{
277 		return gtk_selection_model_unselect_item(getSelectionModelStruct(), position) != 0;
278 	}
279 
280 	/**
281 	 * Requests to unselect a range of items in the model.
282 	 *
283 	 * Params:
284 	 *     position = the first item to unselect
285 	 *     nItems = the number of items to unselect
286 	 *
287 	 * Returns: %TRUE if this action was supported and no fallback should be
288 	 *     tried. This does not mean the range was unselected.
289 	 */
290 	public bool unselectRange(uint position, uint nItems)
291 	{
292 		return gtk_selection_model_unselect_range(getSelectionModelStruct(), position, nItems) != 0;
293 	}
294 
295 	/**
296 	 * Emitted when the selection state of some of the items in @model changes.
297 	 *
298 	 * Note that this signal does not specify the new selection state of the
299 	 * items, they need to be queried manually. It is also not necessary for
300 	 * a model to change the selection state of any of the items in the selection
301 	 * model, though it would be rather useless to emit such a signal.
302 	 *
303 	 * Params:
304 	 *     position = The first item that may have changed
305 	 *     nItems = number of items with changes
306 	 */
307 	gulong addOnSelectionChanged(void delegate(uint, uint, SelectionModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
308 	{
309 		return Signals.connect(this, "selection-changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
310 	}
311 }